package cn.Liujialin.org.controller;


import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.org.domain.Shop;
import cn.Liujialin.org.query.ShopQuery;
import cn.Liujialin.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;


    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 修改对象
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.save(shop);
            }else{
                shopService.update(shop);
            }

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping
    public List<Shop> list(){

        return shopService.findByAll();
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }

    /**
     * 添加对象的教研
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */

    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.fail("用户名已用有");
        } catch (Exception e) { //500
            e.printStackTrace();
            return AjaxResult.me().fail("系统繁忙");
        }
    }
}
